package com.questionnaires.service;

import com.questionnaires.exception.NotExistsException;
import com.questionnaires.exception.NotUniqueException;
import com.questionnaires.model.Category;
import com.questionnaires.repository.CategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author rominamilea@gmail.com
 */

@Service(value="categoryService")
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryRepository categoryRepository;

    /**
     * {@inheritDoc}
     */
    @Transactional
    public void saveOrUpdate(Category category) throws NotUniqueException {
        categoryRepository.saveOrUpdate(category);
    }

    /**
     * {@inheritDoc}
     */
    @Transactional
    public void delete(long id) throws NotExistsException {
        categoryRepository.delete(id);
    }

    /**
     * {@inheritDoc}
     */
    @Transactional
    public List<Category> findAll() {
        return categoryRepository.getCategories();
    }

    public CategoryRepository getCategoryRepository() {
        return categoryRepository;
    }

    public void setCategoryRepository(CategoryRepository categoryRepository) {
        this.categoryRepository = categoryRepository;
    }
}
